<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :    01/04/2010
// version :    02/04/2010

abstract class Cmis_Client_Services_Abstract implements Cmis_Client_Services_Interface
{
    protected $_ACLServices;
    protected $_DiscoveryServices;
    protected $_MultiFilingServices;
    protected $_NavigationServices;
    protected $_ObjectServices;
    protected $_PolicyServices;
    protected $_RelationshipServices;
    protected $_RepositoryServices;
    protected $_VersionningServices;

    protected $_protocol_ACLServices;
    protected $_protocol_DiscoveryServices;
    protected $_protocol_MultiFilingServices;
    protected $_protocol_NavigationServices;
    protected $_protocol_ObjectServices;
    protected $_protocol_PolicyServices;
    protected $_protocol_RelationshipServices;
    protected $_protocol_RepositoryServices;
    protected $_protocol_VersionningServices;

    protected $_cmis_repository_type;

    /**
     * eg : Cmis_Client_Rest_Generic
     * @var string
     */
    protected $_cmis_client;

    /**
     * eg : Cmis_Client_Services_Generic
     * @var string
     */
    protected $_cmis_client_services = 'Cmis_Client_Services_Generic';

	/**
	 * URL of the endpoint starting service
	 * @var String
	 */
	protected $_endpoint;

	/**
	 * @var Cmis_Request_Interface
	 */
	protected $_protocol;

    public function __construct(Cmis_Request_Interface $protocol,  $endpoint, $type = 'Generic')
    {
        $this->setEndpoint($endpoint);
        $this->_protocol = $protocol;

        $this->_cmis_repository_type = $type;

        //init services
        $this->start();

        //following the CMIS spect, alltime start with a getRepository
        $this->getService('Repository')->getRepositories($this->getEndpoint());
    }

    public function getType()
    {
    	return $this->_cmis_repository_type;
    }

	public function getProtocol()
	{
		return $this->_protocol;
	}

	public function hasMultipleRepository()
	{
		return (count($this->getService('Repository')->getRepositories($this->getEndpoint())) > 1);
	}

	public function setEndpoint($url)
	{
		$this->_endpoint = $url;
		return $this;
	}

	public function getEndpoint()
	{
		return $this->_endpoint;
	}

	public function setService($service_name, Cmis_Services_Interface $services)
	{
		$property = '_' . $service_name . 'Services';
		$method = 'set' . $property;

		if (method_exists($this, $method))
		{
			$this->$method($services);
		}
		elseif(property_exists(__CLASS__, $property))
		{
			$this->$property = $services;
		}

		return $this;
	}

	public function getService($service_name)
	{
		$property = '_' . $service_name . 'Services';
		$method = 'get' . $property;

		if (method_exists($this, $method))
		{
			return $this->$method();
		}
		elseif(property_exists(__CLASS__, $property))
		{
			return $this->$property;
		}

		return null;
	}

    public function setProtocolService($service_name, Cmis_Services_Protocol_Interface $services)
    {
    	return $this->setService('protocol_' .$service_name, $services);
    }

    public function getProtocolService($service_name)
    {
        return $this->getService('protocol_' . $service_name);
    }

	//
	// No-interface method
	//

    /**
     * Start the CMIS services
     * @return Cmis_Client_Services_Interface
     */
    public function start()
    {
        $this->_startProtocolService('Repository', $this->_cmis_client . '_Repository');
        $this->_startService('Repository', $this->_cmis_client_services . '_Repository');
        $this->_startProtocolService('Discovery', $this->_cmis_client . '_Discovery');
        $this->_startService('Discovery', $this->_cmis_client_services . '_Discovery');
        $this->_startProtocolService('ACL', $this->_cmis_client . '_ACL');
        $this->_startService('ACL', $this->_cmis_client_services . '_ACL');
        $this->_startProtocolService('MultiFiling', $this->_cmis_client . '_MultiFiling');
        $this->_startService('MultiFiling', $this->_cmis_client_services . '_MultiFiling');
        $this->_startProtocolService('Navigation', $this->_cmis_client . '_Navigation');
        $this->_startService('Navigation', $this->_cmis_client_services . '_Navigation');
        $this->_startProtocolService('Policy', $this->_cmis_client . '_Policy');
        $this->_startService('Policy', $this->_cmis_client_services . '_Policy');
        $this->_startProtocolService('Object', $this->_cmis_client . '_Object');
        $this->_startService('Object', $this->_cmis_client_services . '_Object');
        $this->_startProtocolService('Relationship', $this->_cmis_client . '_Relationship');
        $this->_startService('Relationship', $this->_cmis_client_services . '_Relationship');
        $this->_startProtocolService('Versionning', $this->_cmis_client . '_Versionning');
        $this->_startService('Versionning', $this->_cmis_client_services . '_Versionning');

        return $this;
    }

    /**
     * Make a new instance of a class and at it to the CMIS services
     * if service with the _cmis_repository_type exist use it
     * @param string $name
     * @param string $className
     * @return void
     */
    protected function _startService($name, $className)
    {
    	if ($this->_cmis_repository_type != 'Generic')
    	{
    		$_className = str_replace('_Generic_', '_' . $this->_cmis_repository_type . '_', $className);
    		if (class_exists($_className, true))
    		{
    			$className = $_className;
    		}
    	}
        $this->setService($name, new $className($this));
    }

    /**
     * Make a new instance of a class and at it to the CMIS protocol services
     * @param string $name
     * @param string $className
     * @return void
     */
    protected function _startProtocolService($name, $className)
    {
        $this->setProtocolService($name, new $className($this));
    }
}

# EOF
